package com.tanhua.admin.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.admin.service.SystemService;
import com.tanhua.admin.utils.AdminUserThreadLocal;
import com.tanhua.admin.utils.CodeUtils;
import com.tanhua.admin.utils.NoAuthorization;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName SystemController
 * @Description TODO
 * @Author JavaDev
 * @Date 2021/1/16 18:11
 * @Version 1.0
 */
@Controller
@RequestMapping("/system/users")
@Slf4j
public class SystemController {

    @Autowired
    private SystemService systemService;


    /**
     * 前端获取验证码
     *
     * @param uuid
     * @param request
     * @param response
     * @return
     */
    @NoAuthorization
    @GetMapping("/verification")
    public ResponseEntity getVerification(@RequestParam("uuid") String uuid, HttpServletRequest request, HttpServletResponse response) {
        try {

            String code = CodeUtils.getCode(request, response);// 生成随机的验证码图片并返回前端

            return ResponseEntity.ok(null);
        } catch (Exception e) {
            log.error("获取验证码出错", e);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 用户登录 的接口
     *
     * @param params 包含四个参数 username,password,verificationCode,uuid
     * @return
     */
    @NoAuthorization
    @PostMapping("/login")
    public ResponseEntity<Object> login(@RequestBody Map<String, String> params, HttpServletRequest request, HttpServletResponse response) {


        try {
            String username = params.get("username");
            String password = params.get("password");
            String verificationCode = params.get("verificationCode");
            String uuid = params.get("uuid");


            System.out.println("设置的验证码为:" + request.getSession().getAttribute("VER_CODE_SERVER"));
            String code = (String) request.getSession().getAttribute("VER_CODE_SERVER");
            if (!StringUtils.equalsIgnoreCase(verificationCode, code)) { // 验证码校验未通过
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("验证码校验未通过,请刷新验证码");
            }
            // 验证码校验成功后,校验用户密码是否正确
            Map<String, Object> result = systemService.login(username, password);
            if (result == null) {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("用户名 或 密码 错误");
            }


            return ResponseEntity.ok(result);
        } catch (
                Exception e) {
            log.error("用户登录 的接口出现异常", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("接口出现异常");
    }

    @PostMapping("/profile")
    public ResponseEntity<Object> profile() {
        return ResponseEntity.ok(AdminUserThreadLocal.get());
    }

    @PostMapping("/logout")
    public ResponseEntity<Object> logout() {
        if (systemService.logout()) {
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("用户登出失败");
    }
}